package com.yonyou.state.core.impl;

import com.yonyou.state.constant.SOStatusEnum;
import com.yonyou.state.core.AbstractSOOperator;
import com.yonyou.state.core.annotation.SOOperator;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 创建工单操作状态流转
 * @author lidwt
 * @date 2021/10/29 17:50
 **/
@Component
@SOOperator
public class CloseSOOperator extends AbstractSOOperator {

    public CloseSOOperator() {
        super.setStatus(9);
    }
    final int[] supportCurrentStates = {
            SOStatusEnum.confirmed.getStatusOrder(),
            SOStatusEnum.dispatched.getStatusOrder(),
            SOStatusEnum.received.getStatusOrder(),
            SOStatusEnum.reported.getStatusOrder(),
            SOStatusEnum.completed.getStatusOrder()
    };
    public int[] getSupportStates() {
        return supportCurrentStates;
    }
    /**
     * 状态流转方法
     * @param event 流转的工单操作事件
     * @param soStatus 当前工单状态
     * @return 扭转后的工单状态
     */
    @Override
    public int handleEvent(int soStatus, SOStatusEnum event) {
        if (Arrays.binarySearch(getSupportStates(), soStatus) < 0) {
            throw new IllegalArgumentException(String.format("create operation can't handle the status: %s", soStatus));
        }
        System.out.println("进入工单异常关闭状态扭转处理器...");
        switch (event) {
            case closed:
                return SOStatusEnum.closed.getStatusOrder();
            default:
                return getStatus();
        }
    }
}
